﻿using System.Threading;
using System;
using System.Collections.Generic;
using System.Text;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.Automation;
using Echo.Core.UltiProNet;
using Gallio.Framework;
using MbUnit.Framework;
using MbUnit.Framework.ContractVerifiers;
using Echo.Automation.Compliance;
using System.Data.SqlClient;
using Echo.Core.Database.DatabaseDriver;
using System.Diagnostics;

namespace Echo.Automation.Compliance.BusinessRules.RetroSui
{
    [TestFixture]
    [Author("Brent Crews", "Brent_Crews@ultimatesoftware.com")]
    [Category("retro Calcs on the Web")]
    public class dotnetRetroCalcs : AutomatedTest
    {
        UltiPro UltiPro;
        
        public IDatabaseDriverAdapter CompanyDatabaseDriver { get; set; }
        
        [FixtureSetUp]
        public void Setup()
        {
            this.CompanyDatabaseDriver = ConfigDatabase();

            //first turn on retro calcs
            //turn on retro calcs
            string SqlQueryNm_setup__1 = @"exec sp_usg_CompOptionsActive_Add 'RetroCalcs', 'Active'";
            this.CompanyDatabaseDriver.Execute(SqlQueryNm_setup__1);
            string SqlQueryNm_setup__2 = @"exec HRMS_RcPopulateQtrTaxRates";
            this.CompanyDatabaseDriver.Execute(SqlQueryNm_setup__2);
            string SqlQueryNm_setup__3 = @"exec HRMS_RcRecalcRtd";
            this.CompanyDatabaseDriver.Execute(SqlQueryNm_setup__3);
            string SqlQueryNm_setup__4 = @"exec HRMS_RcPushRTDToBatch";
            //this.CompanyDatabaseDriver.Execute(SqlQueryNm_setup__4);

            UltiPro = Launch<UltiPro>();
        }

        [FixtureTearDown]
        public void FixtureTearDown()
        {
            UltiPro.Browser.Quit(); //quit browser and reopen for very next run THAT is because between each run need to run IISreset and correspondingly then HAVE to reopen browser (regardless because of iisreset)
        }

        [TearDown]
        public void TearDown()
        {
            UltiPro.QuickLinks.Logout.Click();
        }

        [Test(Order = 1)]
        [Row("", "", "", "COSUIER", "Web Payroll Company", "4", "01/01/2012", "EDIT")]
        [Row("WORKP", "", "", "CASUIER", "Web Payroll Company", "8.2", "01/10/2012", "RATE")]
        [Row("", "TXFIL", "", "NJSUIER", "Web Payroll Company", "1.25", "01/15/2012", "TXCODE")]
        public void retroMsgBatchMsgCanAddFutureRt(string workpKeyVal, string txfilKeyVal, string retroCalcsParamVal, string taxCode, string companyName, string contrrateVal, string past_dateVal, string reasonVal)
        {
            openBusinessRulesLink(workpKeyVal, txfilKeyVal, retroCalcsParamVal);

            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, past_dateVal, reasonVal);

            if (workpKeyVal.Length == 0)
                UltiPro.Pages.TaxCodeContributionRate.ErrorMessages.Has("The effective date entered is retroactive. To configure retroactive changes, go to Company Tax Setup in the Backoffice (Company Tax Setup > State > Choose State SUI Tax Code).");
            else
                UltiPro.Pages.TaxCodeContributionRate.ErrorMessages.Has("The effective date entered is retroactive. Please contact your Named Service Representative to set up the retroactive rate and update the necessary records.");

            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(fde => fde.Cancel);
            UltiPro.Alert.Accept();

            createManualCheckOneRandomEE(taxCode, companyName);
            
            Int32 lastPerControlDayInBatch = getLastPercDayInBatch(taxCode, companyName);
            
            Int32 lastPerControlDayInBatchMinusOneDay = lastPerControlDayInBatch - 10;
            string lpcdibmod_changedString = manipulatePercToString(lastPerControlDayInBatchMinusOneDay);

            UltiPro.MenuBar.SystemAdmin.Click(subfunct => subfunct.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(fnd => fnd.BusinessRulesNameFilter, FilterType.StartsWith, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(0).Click(frstRw => frstRw.BusinessRulesLink);
            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, lpcdibmod_changedString, reasonVal);

            UltiPro.Pages.TaxCodeContributionRate.ErrorMessages.Has("This tax code exists in a current payroll batch. Please contact your Named Service Representative to setup the rate and update the necessary records.");

            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(fde => fde.Cancel);
            UltiPro.Alert.Accept();

            Int32 lastPerControlDayInBatchPlusOneDay = lastPerControlDayInBatch + 10;
            string lpcdibpod_changedString = manipulatePercToString(lastPerControlDayInBatchPlusOneDay);

            UltiPro.MenuBar.SystemAdmin.Click(subfunct => subfunct.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(fnd => fnd.BusinessRulesNameFilter, FilterType.StartsWith, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(0).Click(frstRw => frstRw.BusinessRulesLink);
            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, lpcdibpod_changedString, reasonVal);

            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(fnd => fnd.CodeFilter, FilterType.StartsWith, taxCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(0).Exists(fnd => fnd.Company);
            //verifyRateWentThrough(taxCode, companyName, DateTime.Parse(lpcdibpod_changedString).Year.ToString());
            
        }


        
        [Test(Order = 2)]
        [Row("", "TXFIL", "RetroCalcs", "NYSUIER", "Web Payroll Two Company", "7.5", "02/01/2012", "ADD")]
        [Row("WORKP", "", "RetroCalcs", "NESUIER", "Web Payroll Company", "1.1", "01/21/2012", "TXCODE")]
        public void retroMsgNOBatchMsgCanAddFutureRt(string workpKeyVal, string txfilKeyVal, string retroCalcsParamVal, string taxCode, string companyName, string contrrateVal, string past_dateVal, string reasonVal)
        {
            openBusinessRulesLink(workpKeyVal, txfilKeyVal, retroCalcsParamVal);

            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, past_dateVal, reasonVal);

            if (workpKeyVal.Length == 0)
                UltiPro.Pages.TaxCodeContributionRate.ErrorMessages.Has("The effective date entered is retroactive. To configure retroactive changes, go to Company Tax Setup in the Backoffice (Company Tax Setup > State > Choose State SUI Tax Code).");
            else
                UltiPro.Pages.TaxCodeContributionRate.ErrorMessages.Has("The effective date entered is retroactive. Please contact your Named Service Representative to set up the retroactive rate and update the necessary records.");
            
            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(fde => fde.Cancel);
            UltiPro.Alert.Accept();
          
            createManualCheckOneRandomEE(taxCode, companyName);

            Int32 lastPerControlDayInBatch = getLastPercDayInBatch(taxCode, companyName);

            Int32 lastPerControlDayInBatchMinusOneDay = lastPerControlDayInBatch - 10;
            string lpcdibmod_changedString = manipulatePercToString(lastPerControlDayInBatchMinusOneDay);

            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, lpcdibmod_changedString, reasonVal);

            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(fnd => fnd.CodeFilter, FilterType.StartsWith, taxCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(0).Exists(fnd => fnd.Company);
            //verifyRateWentThrough(taxCode, companyName, DateTime.Parse(lpcdibpod_changedString).Year.ToString());  //save goes through for (in between where there would be a) batch msg

            Int32 lastPerControlDayInBatchPlusOneDay = lastPerControlDayInBatch + 10;
            string lpcdibpod_changedString = manipulatePercToString(lastPerControlDayInBatchPlusOneDay);

            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, lpcdibpod_changedString, reasonVal);

            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(fnd => fnd.CodeFilter, FilterType.StartsWith, taxCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(0).Exists(fnd => fnd.Company);
            //verifyRateWentThrough(taxCode, companyName, DateTime.Parse(lpcdibpod_changedString).Year.ToString());
        }

        [Test(Order = 3)]
        [Row("", "", "RetroCalcs", "NESUIER", "Web Payroll Company", "9.5", "02/01/2012", "TXCODE")]
        public void canAddANYRt(string workpKeyVal, string txfilKeyVal, string retroCalcsParamVal, string taxCode, string companyName, string contrrateVal, string past_dateVal, string reasonVal)
        {
            openBusinessRulesLink(workpKeyVal, txfilKeyVal, retroCalcsParamVal);

            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, past_dateVal, reasonVal);

            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(fnd => fnd.CodeFilter, FilterType.StartsWith, taxCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(0).Exists(fnd => fnd.Company);
            verifyRateWentThrough(taxCode, companyName, DateTime.Parse(past_dateVal).Year.ToString()); //save goes through for RETRO rate TOO!
            
            createManualCheckOneRandomEE(taxCode, companyName);
          
            Int32 lastPerControlDayInBatch = getLastPercDayInBatch(taxCode, companyName);

            Int32 lastPerControlDayInBatchMinusOneDay = lastPerControlDayInBatch - 10;
            string lpcdibmod_changedString = manipulatePercToString(lastPerControlDayInBatchMinusOneDay);

            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, lpcdibmod_changedString, reasonVal);

            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(fnd => fnd.CodeFilter, FilterType.StartsWith, taxCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(0).Exists(fnd => fnd.Company);
            //verifyRateWentThrough(taxCode, companyName, DateTime.Parse(lpcdibpod_changedString).Year.ToString());  //save goes through for (in between where there would be a) batch msg

            Int32 lastPerControlDayInBatchPlusOneDay = lastPerControlDayInBatch + 10;
            string lpcdibpod_changedString = manipulatePercToString(lastPerControlDayInBatchPlusOneDay);

            repeatClickOnPrevGrid(taxCode, companyName, contrrateVal, lpcdibpod_changedString, reasonVal);

            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(fnd => fnd.CodeFilter, FilterType.StartsWith, taxCode);
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(0).Exists(fnd => fnd.Company);
            //verifyRateWentThrough(taxCode, companyName, DateTime.Parse(lpcdibpod_changedString).Year.ToString());
        }

        [Test(Order = 4)]
        [Row("WORKP", "TXFIL", "", "LASUIER", "Web Payroll Company")]
        [Row("WORKP", "TXFIL", "RetroCalcs", "GASUIER", "Web Payroll Company")]
        public void viewOnly(string workpKeyVal, string txfilKeyVal, string retroCalcsParamVal, string taxCode, string companyName)
        {
            openBusinessRulesLink(workpKeyVal, txfilKeyVal, retroCalcsParamVal);

            navigateToTaxContrPage(taxCode, companyName);

            UltiPro.Pages.TaxCodeContributionRate.NotEnabled(kj => kj.ContributionRateValue);
        }
        



        #region Helper methods
        private string manipulatePercToString(Int32 intInput)
        {
            string straightString = intInput.ToString();
            string changedString = straightString.Substring(4, 2) + "/" + straightString.Substring(6, 2) + "/" + straightString.Substring(0, 4);

            return changedString;
        }

        private string manipulateStringToPerc(string straightString)
        {
            
            int firstSlash = straightString.IndexOf("/", 0);
            int secondSlash = straightString.IndexOf("/", firstSlash+1);

            string month = straightString.Substring(0, firstSlash);
            if (month.Length == 1)
            {
                month = "0" + month;
            }
            string day = straightString.Substring(firstSlash + 1, secondSlash - firstSlash - 1);
            if (day.Length == 1)
            {
                day = "0" + day;
            }
            string year = straightString.Substring(straightString.Length - 4, 4);


            string changedString = year + month + day + "1";

            return changedString;
        }

        private void openBusinessRulesLink(string workpKeyVal, string txfilKeyVal, string retroCalcsParamVal)
        {
            updateKeys(workpKeyVal, txfilKeyVal, retroCalcsParamVal);

            UltiPro.Pages.Login.LoginAs("usa-canu", "password");

            UltiPro.MenuBar.SystemAdmin.Click(subfunct => subfunct.BusinessRules);
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Find(fnd => fnd.BusinessRulesNameFilter, FilterType.StartsWith, "Taxes");
            UltiPro.Pages.BusinessRulesSummary.BusinessRulesSummaryGrid.Row(0).Click(frstRw => frstRw.BusinessRulesLink);
        }

        private void repeatClickOnPrevGrid(string taxCodeName, string companyName, string contribVal, string dateVal, string reasonVal)
        {
            navigateToTaxContrPage(taxCodeName, companyName);
            
            UltiPro.Pages.TaxCodeContributionRate.Set(fd => fd.ContributionRateValue, contribVal);

            UltiPro.Pages.TaxCodeContributionRate.Set(setTo => setTo.EmployerIdNumberTextBox, "8.95");

            UltiPro.Pages.TaxCodeContributionRate.EffectiveDatePicker.Set(DateTime.Parse(dateVal));

            UltiPro.Pages.TaxCodeContributionRate.Set(fde => fde.RateChangeReasonDropDown, reasonVal);

            UltiPro.Pages.TaxCodeContributionRate.ButtonBar.Click(fde => fde.Save);
 
        }

        private void navigateToTaxContrPage(string taxCodeName, string companyName)
        {
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Find(fnd => fnd.CodeFilter, FilterType.StartsWith, taxCodeName);
            
            UltiPro.Pages.BusinessRuleDetail.BusinessRuleDetailGrid.Row(fnd => fnd.Company, companyName).Click(frstRw => frstRw.Code);
        }

        private Int32 getLastPercDayInBatch(string taxCodeName, string companyName)
        {
            Int32 returnVal = 0;

            string SqlQueryName1;
            SqlQueryName1 = "select max(tbtpercontrol) from t_batch where tbttaxcode = '" + taxCodeName + "' and tbtcoid in (select cmpcoid from company where cmpcompanyname = '" + companyName + "')";
            string temp = this.CompanyDatabaseDriver.GetValue(SqlQueryName1);
            returnVal = Int32.Parse(temp);

            return returnVal;
        }

        private void updateKeys(string workpCode, string txfilCode, string retroCalcsCompOptFlag)
        {
            string workpCode_singleQuotes = "'" + workpCode + "'";
            string txfilCode_singleQuotes = "'" + txfilCode + "'";
            string retroCalcsCompOptFlag_singleQuotes = "'" + retroCalcsCompOptFlag + "'";

            
            //if the 1st string parameter is blank then delete from productkeys for workp active flag otherwise insert that new row
            string SqlQueryName1_2;
            SqlQueryName1_2 = "begin transaction IF (select len(" + workpCode_singleQuotes + ")) > 0 begin set identity_insert rbsProductKeys ON IF NOT EXISTS (select * from rbsProductKeys where prkProdCode = " + workpCode_singleQuotes + ") begin insert into rbsProductKeys (prkProdCode, prkProdKey, prkID) values (" + workpCode_singleQuotes + ", 'r8qThD6zubYrcReMDFXxYw==', '57') end IF NOT EXISTS (select * from ProductKeys where prkProdCode = " + workpCode_singleQuotes + ") begin insert into ProductKeys (prkProdCode, prkProdKey) values (" + workpCode_singleQuotes + ", 0x802CC5B255E10782000000000000800000000000) end  end commit";
            this.CompanyDatabaseDriver.Execute(SqlQueryName1_2);

            string SqlQueryName1;
            SqlQueryName1 = "begin transaction IF (select len(" + workpCode_singleQuotes + ")) = 0 begin delete from rbsProductKeys where prkprodcode = 'WORKP' delete from ProductKeys where prkprodcode =  'WORKP' end commit";
            this.CompanyDatabaseDriver.Execute(SqlQueryName1);


            //if the 2nd string parameter is blank then delete from productkeys for txfil active flag otherwise insert that new row
            string SqlQueryName2_2;
            SqlQueryName2_2 = "begin transaction IF (select len(" + txfilCode_singleQuotes + ")) > 0 begin set identity_insert rbsProductKeys ON IF NOT EXISTS (select * from rbsProductKeys where prkProdCode = " + txfilCode_singleQuotes + ") begin insert into rbsProductKeys (prkProdCode, prkProdKey, prkID) values (" + txfilCode_singleQuotes + ", 'y8qThD6zubYrcReMDFXxYw==', '67') end IF NOT EXISTS (select * from ProductKeys where prkProdCode = " + txfilCode_singleQuotes + ") begin insert into ProductKeys (prkProdCode, prkProdKey) values (" + txfilCode_singleQuotes + ", 0x802CC8B255E10782000000000000800000000000) end  end commit";
            this.CompanyDatabaseDriver.Execute(SqlQueryName2_2);

            string SqlQueryName2;
            SqlQueryName2 = "begin transaction IF (select len(" + txfilCode_singleQuotes + ")) = 0 begin delete from rbsProductKeys where prkprodcode = 'TXFIL' delete from ProductKeys where prkprodcode =  'TXFIL' end commit";
            this.CompanyDatabaseDriver.Execute(SqlQueryName2);


            //if the 3rd string parameter is blank then delete from compoptions active flag otherwise insert a new row
            string SqlQueryName3_2;
            SqlQueryName3_2 = "begin transaction IF (select len(" + retroCalcsCompOptFlag_singleQuotes + ")) > 0 begin IF NOT EXISTS (select * from CompOptionsActive where coaGroup = " + retroCalcsCompOptFlag_singleQuotes + ") begin insert into CompOptionsActive (coaDateTimeCreated, coaGroup, coaOptionID) values ('2012-05-21 14:19:38.497', " + retroCalcsCompOptFlag_singleQuotes + ", 'Active') end  end commit";
            this.CompanyDatabaseDriver.Execute(SqlQueryName3_2);

            string SqlQueryName3;
            SqlQueryName3 = "begin transaction IF (select len(" + retroCalcsCompOptFlag_singleQuotes + ")) = 0 begin delete from CompOptionsActive where coagroup = 'RetroCalcs' end commit";
            this.CompanyDatabaseDriver.Execute(SqlQueryName3);


            Process p = new Process();
            // run iisreset for the webserver used in the configuration file (by stripping off the first 7 characters ("http://") of it)
            p.StartInfo.UseShellExecute = true;
            p.StartInfo.RedirectStandardOutput = false;
            p.StartInfo.FileName = "iisreset";
            p.StartInfo.Arguments = Environment.DefaultWebServer.Substring(7, Environment.DefaultWebServer.Length - 7);
            Thread.Sleep(6200);
            p.Start();
            p.WaitForExit();

            Thread.Sleep(2200);
        }

        public void createManualCheckOneRandomEE(string taxCodeName, string companyName)
        {

            string SqlQueryName1 = "select top 1 tbtempno from t_batch as t1 where tbttaxcode = '" + taxCodeName + "' and tbtcoid in (select cmpcoid from company where cmpcompanyname = '" + companyName + "') and tbtpercontrol = (select max(tbtpercontrol) from t_batch as t2 where tbttaxcode = '" + taxCodeName + "' and tbtcoid in (select cmpcoid from company where cmpcompanyname = '" + companyName + "' and t1.tbteeid = t2.tbteeid)) and 1 = (select count(eeccoid) from empcomp where eeceeid = tbteeid) and exists (select 1 from empcomp where eecempno = tbtempno and EecPayGroup like 'WEBPR%')";
            string empNoToUse = this.CompanyDatabaseDriver.GetValue(SqlQueryName1);

            string SqlQueryName2 = "select distinct EecPayGroup from empcomp where EecEmpNo = '" + empNoToUse + "'";
            string paygroupToUse = this.CompanyDatabaseDriver.GetValue(SqlQueryName2);

            UltiPro.MenuBar.PayrollProcessing.Click(link => link.PayrollGateway);
            UltiPro.Pages.PayrollGateway.PayrollGatewayGrid.Find(search => search.PayGroupCode, FilterType.Is, paygroupToUse);
            UltiPro.Pages.PayrollGateway.PayrollGatewayGrid.Row(paygroupToUse).Click(link => link.PayGroupLink);
            UltiPro.Pages.PayrollOverview.ThingsICanDo.Click(link => link.ViewEditPayrollLink);

            UltiPro.Pages.WPREmployeePaySummary.ActionToolbar.Set(atr => atr.ActionDropDown, "AddInstantPays()");

            UltiPro.Pages.WPREmployeePaySummary.WPREmployeePaySummaryGrid.Find(grt => grt.EmployeeNumber, FilterType.StartsWith, empNoToUse);

            UltiPro.Pages.WPREmployeePaySummary.WPREmployeePaySummaryGrid.Row(0).Click(grt => grt.EmployeeLink);

            string SqlQueryName3 = "select distinct eepnamelast from emppers where Eepeeid in (select eeceeid from empcomp where eecempno = '" + empNoToUse + "')";
            string personsLastName = this.CompanyDatabaseDriver.GetValue(SqlQueryName3);

            UltiPro.Browser.SwitchToWindow(personsLastName);

            string SqlQueryName4 = "select max(tbtpercontrol) from t_batch where tbttaxcode = '" + taxCodeName + "' and tbtcoid in (select cmpcoid from company where cmpcompanyname = '" + companyName + "')";
            string maxPerControlBeforeSaving = this.CompanyDatabaseDriver.GetValue(SqlQueryName4);
            DateTime maxPerControlBeforeSaving_slashes = DateTime.Parse(manipulatePercToString(Int32.Parse(maxPerControlBeforeSaving)));
            maxPerControlBeforeSaving_slashes.AddDays(3);
            string newPercToUse = manipulateStringToPerc(maxPerControlBeforeSaving_slashes.ToShortDateString());

            UltiPro.Pages.EePayrollPayCheckDetail.ButtonBar.Click(aso => aso.Save);
            Thread.Sleep(6200);
            UltiPro.Pages.EePayrollPayCheckDetail.ButtonBar.Click(asn => asn.Close);

            string SqlQueryName5_a = "update a_batch set abtpercontrol = '" + newPercToUse + "' where abtempno = '" + empNoToUse + "' and abtgennumber = (select max(abtgennumber) from a_batch where abtpercontrol = '" + newPercToUse + "')";
            this.CompanyDatabaseDriver.Execute(SqlQueryName5_a);
            string SqlQueryName5_b = "update b_batch set bbtpercontrol = '" + newPercToUse + "' where bbtempno = '" + empNoToUse + "' and bbtgennumber = (select max(bbtgennumber) from b_batch where bbtpercontrol = '" + newPercToUse + "')";
            this.CompanyDatabaseDriver.Execute(SqlQueryName5_b);
            string SqlQueryName5_d = "update d_batch set dbtpercontrol = '" + newPercToUse + "' where dbtempno = '" + empNoToUse + "' and dbtgennumber = (select max(dbtgennumber) from d_batch where dbtpercontrol = '" + newPercToUse + "')";
            this.CompanyDatabaseDriver.Execute(SqlQueryName5_d);
            string SqlQueryName5_e = "update e_batch set ebtpercontrol = '" + newPercToUse + "' where ebtempno = '" + empNoToUse + "' and ebtgennumber = (select max(ebtgennumber) from e_batch where ebtpercontrol = '" + newPercToUse + "')";
            this.CompanyDatabaseDriver.Execute(SqlQueryName5_e);
            string SqlQueryName5_m = "update m_batch set mbtpercontrol = '" + newPercToUse + "' where mbtempno = '" + empNoToUse + "' and mbtgennumber = (select max(mbtgennumber) from m_batch where mbtpercontrol = '" + newPercToUse + "')";
            this.CompanyDatabaseDriver.Execute(SqlQueryName5_m);
            string SqlQueryName5_t = "update t_batch set tbtpercontrol = '" + newPercToUse + "' where tbtempno = '" + empNoToUse + "' and tbtgennumber = (select max(tbtgennumber) from t_batch where tbtpercontrol = '" + newPercToUse + "')";
            this.CompanyDatabaseDriver.Execute(SqlQueryName5_t);

            UltiPro.Browser.SwitchToMainWindow();

            //UltiPro.Browser.SwitchToWindow("US");
        }

        private void verifyRateWentThrough(string taxCode, string companyName, string datesYear)
        {
            string SqlQueryName1 = "select cmpcoid from company where cmpcompanyname = '" + companyName + "'";
            string coidFromCompanyName = this.CompanyDatabaseDriver.GetValue(SqlQueryName1);

            string convertToPCstart = datesYear + "01010";

            if (Int32.Parse(convertToPCstart) < 2011)
            {
                convertToPCstart = "201101010";
            }
            else if (Int32.Parse(convertToPCstart) > Int32.Parse(DateTime.Now.Year.ToString()))
            {
                convertToPCstart = DateTime.Now.Year.ToString() + "01010";
            }


            int countOfReturn = 0;
            string SqlQueryName2 = "select count(*) from RcTEventQ where coid = '" + companyName + "' and taxcode = '" + taxCode + "' and taxyear = '" + datesYear + "' and PCStart = '" + convertToPCstart + "'";
            countOfReturn = Int32.Parse(this.CompanyDatabaseDriver.GetValue(SqlQueryName2));

            UltiPro.Equals(countOfReturn, 1);   //this line of code is the only line of code for ULTI-76512; everything else in here (except for UltiPro.Pages.TaxCodeContributionRate.ErrorMessages.Has ) is for ulti-70628
            //should only return 1 row regardless (shouldnt ever add a 2nd row to this even if testing this twice
        }
        #endregion


    }
}
